<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>grep</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_001_014_890">&nbsp;</a>NAME</h4><blockquote>
grep - search a file for a pattern
</blockquote><h4><a name = "tag_001_014_891">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

grep <b>[</b> -E| -F<b>][</b> -c| -l| -q <b>][</b>-insvx<b>] </b>-e <i>pattern_list...
</i><b>[</b>-f <i>pattern_file</i><b>]</b>...<b>[</b><i>file</i>...<b>]</b>

grep <b>[</b> -E| -F<b>][</b> -c| -l| -q <b>][</b>-insvx<b>][</b>-e <i>pattern_list...
</i>-f <i>pattern_file</i>...<b>[</b><i>file</i>...<b>]</b>

grep <b>[</b> -E| -F<b>][</b> -c| -l| -q <b>][</b>-insvx<b>] </b><i>pattern_list</i><b>[</b><i>file</i>...<b>]</b>
</code>
</pre>
</blockquote><h4><a name = "tag_001_014_892">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>grep</i>
utility searches the input files,
selecting lines matching one or more patterns;
the types of patterns are controlled by the options specified.
The patterns are specified by the
<b>-e</b>
option,
<b>-f</b>
option, or the
<i>pattern_list</i>
operand.
The
<i>pattern_list</i>'s
value consists of one
or more patterns separated by
newline characters;
the
<i>pattern_file</i>'s
contents consist of one or more patterns terminated by
newline characters.
By default, an input line will be selected if any
pattern, treated as an entire basic regular expression (BRE) as
described in the <b>XBD</b> specification, <a href="../xbd/re.html#tag_007_003"><b>Basic Regular Expressions</b>&nbsp;</a> , matches any part of the line;
a null BRE will match every line.
By default,
each selected input line will be written to the standard output.
<p>
Regular expression matching will be based on text lines.
Since a newline character
separates or terminates patterns (see the
<b>-e</b>
and
<b>-f</b>
options below), regular expressions cannot contain a
newline
character.
Similarly, since patterns are matched against
individual lines of the input, there is no way for a pattern to
match a newline character found in the input.
</blockquote><h4><a name = "tag_001_014_893">&nbsp;</a>OPTIONS</h4><blockquote>
The
<i>grep</i>
utility supports the <b>XBD</b> specification, <a href="../xbd/utilconv.html#usg"><b>Utility Syntax Guidelines</b>&nbsp;</a> .
<p>
The following options are supported:
<dl compact>

<dt><b>-E</b>
<dd>Match using extended regular expressions.
Treat each pattern specified
as an ERE, as described in the <b>XBD</b> specification, <a href="../xbd/re.html#tag_007_004"><b>Extended Regular Expressions</b>&nbsp;</a> .
If any entire ERE pattern matches an input line, the line will be matched.
A null ERE matches every line.

<dt><b>-F</b>
<dd>Match using fixed strings.
Treat each pattern specified as a string instead of a regular expression.
If an input line contains any of the patterns as a contiguous
sequence of bytes, the line will be matched.
A null string matches every line.

<dt><b>-c</b>
<dd>Write only a count of selected lines to standard output.

<dt><b>-e&nbsp;</b><i>pattern_list</i>
<dd>
Specify one or more patterns to be used during the search for input.
Patterns in
<i>pattern_list</i>
must be separated by a
newline character.
A null pattern can be specified by two adjacent
newline characters in
<i>pattern_list</i>.
Unless the
<b>-E</b>
or
<b>-F</b>
option is also specified, each pattern will be treated as a BRE,
as described in the <b>XBD</b> specification, <a href="../xbd/re.html#tag_007_003"><b>Basic Regular Expressions</b>&nbsp;</a> .
Multiple
<b>-e</b>
and
<b>-f</b>
options are accepted by the
<i>grep</i>
utility.
All of the specified patterns
are used when matching lines, but the order of evaluation
is unspecified.

<dt><b>-f&nbsp;</b><i>pattern_file</i>
<dd>
Read one or more patterns from the file named by the
pathname
<i>pattern_file.</i>
Patterns in
<i>pattern_file</i>
are terminated by a
newline character.
A null pattern can be specified by an empty line in
<i>pattern_file.</i>
Unless the
<b>-E</b>
or
<b>-F</b>
option is also
specified, each pattern will be treated as a BRE, as described in
the <b>XBD</b> specification, <a href="../xbd/re.html#tag_007_003"><b>Basic Regular Expressions</b>&nbsp;</a> .

<dt><b>-i</b>
<dd>Perform pattern matching in searches without regard to case.
See the <b>XBD</b> specification, <a href="../xbd/re.html#tag_007_002"><b>Regular Expression General Requirements</b>&nbsp;</a> .

<dt><b>-l</b>
<dd>(The letter ell.)
Write only the names of files containing selected lines to standard output.
Pathnames are written once per file searched.
If the standard input is searched, a pathname of
<b>(standard&nbsp;input)</b>
will be written, in the POSIX locale.
In other locales,
<b>standard input</b>
may be replaced by something more appropriate in those locales.

<dt><b>-n</b>
<dd>Precede each output line by its relative line number in the file,
each file starting at line 1.
The line number counter will be reset for each file processed.

<dt><b>-q</b>
<dd>Quiet.
Do not write anything to the standard output,
regardless of matching lines.
Exit with zero status
if an input line is selected.

<dt><b>-s</b>
<dd>Suppress the error messages ordinarily written for non-existent
or unreadable files.
Other error messages will not be suppressed.

<dt><b>-v</b>
<dd>Select lines not matching any of the specified patterns.
If the
<b>-v</b>
option is not specified, selected lines will be those that match any of the
specified patterns.

<dt><b>-x</b>
<dd>Consider only input lines that use all characters in
the line to match an entire fixed string or
regular expression to be matching lines.

</dl>
</blockquote><h4><a name = "tag_001_014_894">&nbsp;</a>OPERANDS</h4><blockquote>
The following operands are supported:
<dl compact>

<dt><i>pattern</i><dd>Specify one or more patterns to be used during the
search for input.
This operand is treated as if it were specified as
<b>-e</b>&nbsp;<i>pattern_list</i>.

<dt><i>file</i><dd>A pathname of a file to be searched for the patterns.
If no
<i>file</i>
operands are specified,
the standard input will be used.

</dl>
</blockquote><h4><a name = "tag_001_014_895">&nbsp;</a>STDIN</h4><blockquote>
The standard input will be used only if no
<i>file</i>
operands are specified.
See the INPUT FILES section.
</blockquote><h4><a name = "tag_001_014_896">&nbsp;</a>INPUT FILES</h4><blockquote>
The input files must be text files.
</blockquote><h4><a name = "tag_001_014_897">&nbsp;</a>ENVIRONMENT VARIABLES</h4><blockquote>
The following environment variables affect the execution of
<i>grep</i>:
<dl compact>

<dt><i>LANG</i><dd>Provide a default value for the internationalisation variables
that are unset or null.
If
<i>LANG</i>
is unset or null, the corresponding value from the
implementation-dependent default locale will be used.
If any of the internationalisation variables contains an invalid setting, the
utility will behave as if none of the variables had been defined.

<dt><i>LC_ALL</i><dd>
If set to a non-empty string value,
override the values of all the other internationalisation variables.

<dt><i>LC_COLLATE</i><dd>
Determine the locale for the
behaviour of ranges, equivalence classes
and multi-character collating elements
within regular expressions.

<dt><i>LC_CTYPE</i><dd>
Determine the
locale for the interpretation of sequences of bytes of text data as
characters (for example, single- as opposed to multi-byte characters
in arguments and input files) and
the behaviour of character classes within regular expressions.


<dt><i>LC_MESSAGES</i><dd>
Determine the locale that should be used to affect
the format and contents of diagnostic
messages written to standard error.

<dt><i>NLSPATH</i><dd>
Determine the location of message catalogues
for the processing of
<i>LC_MESSAGES .
</i>
</dl>
</blockquote><h4><a name = "tag_001_014_898">&nbsp;</a>ASYNCHRONOUS EVENTS</h4><blockquote>
Default.
</blockquote><h4><a name = "tag_001_014_899">&nbsp;</a>STDOUT</h4><blockquote>
If the
<b>-l</b>
option is in effect,
and the
<b>-q</b>
option is not, the following will be written
for each file containing at least one selected input line:
<p><code>
<tt>"%s\n"</tt>, <i>file</i>
</code>
<p>
Otherwise, if more than one
<i>file</i>
argument appears,
and
<b>-q</b>
is not specified,
the
<i>grep</i>
utility will prefix each output line by:
<p><code>
<tt>"%s:"</tt>, <i>file</i>
</code>
<p>
The remainder of each output line depends on the other options specified:
<ul>
<p>
<li>
If the
<b>-c</b>
option is in effect, the remainder of each output line will contain:
<p><code>
<tt>"%d\n"</tt>, &lt;<i>count</i>&gt;
</code>
<p>
<li>
Otherwise, if
<b>-c</b>
is not in effect and the
<b>-n</b>
option is in effect, the following
will be written to standard output:
<p><code>
<tt>"%d:"</tt>, &lt;<i>line number</i>&gt;
</code>
<p>
<li>
Finally, the following will be written to standard output:
<p><code>
<tt>"%s"</tt>, &lt;<i>selected-line contents</i>&gt;
</code>
<p>
</ul>
</blockquote><h4><a name = "tag_001_014_900">&nbsp;</a>STDERR</h4><blockquote>
Used only for diagnostic messages.
</blockquote><h4><a name = "tag_001_014_901">&nbsp;</a>OUTPUT FILES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_001_014_902">&nbsp;</a>EXTENDED DESCRIPTION</h4><blockquote>
None.
</blockquote><h4><a name = "tag_001_014_903">&nbsp;</a>EXIT STATUS</h4><blockquote>
The following exit values are returned:
<dl compact>

<dt>0<dd>One or more lines were selected.

<dt>1<dd>No lines were selected.

<dt>&gt;1<dd>An error occurred.

</dl>
</blockquote><h4><a name = "tag_001_014_904">&nbsp;</a>CONSEQUENCES OF ERRORS</h4><blockquote>
If the
<b>-q</b>
option is specified, the exit status will be zero if
an input line is selected, even if an error was detected.
Otherwise, default actions will be performed.
</blockquote><h4><a name = "tag_001_014_905">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
Care should be taken when using characters in
<i>pattern_list</i>
that may also be meaningful to the command interpreter.
It is safest to enclose the entire
<i>pattern_list</i>
argument in single quotes:
<pre>
<code>
'...'
</code>
</pre>
<p>
The
<b>-e</b>&nbsp;<i>pattern_list</i>
option has the same effect as the
<i>pattern_list</i>
operand, but is useful when
<i>pattern_list</i>
begins with the hyphen delimiter.
It is also useful when it is more
convenient to provide multiple patterns as separate arguments.
<p>
Multiple
<b>-e</b>
and
<b>-f</b>
options are accepted and
<i>grep</i>
will use all of the patterns
it is given while matching input text lines.
(Note that the order of evaluation is not specified.
If an implementation
finds a null string as a pattern, it is allowed to use that
pattern first, matching every line, and effectively ignore
any other patterns.)
<p>
The
<b>-q</b>
option provides a means of easily determining whether or not
a pattern (or string) exists in a group of files.
When searching several files, it provides a performance improvement
(because it can quit as soon as it finds the first match) and
requires less care by the user in choosing the set of files to
supply as arguments (because it will exit zero if it finds a
match even if
<i>grep</i>
detected an access or read error on earlier file operands).
</blockquote><h4><a name = "tag_001_014_906">&nbsp;</a>EXAMPLES</h4><blockquote>
<ol>
<p>
<li>
To find all uses of the word
Posix
(in any case) in file
<b>text.mm</b>
and write with line numbers:
<pre>
<code>
grep -i -n posix text.mm
</code>
</pre>
<p>
<li>
To find all empty lines in the standard input:
<pre>
<code>
grep ^$
</code>
</pre>
<p>
or:
<pre>
<code>
grep -v .
</code>
</pre>
<br>
<p>
<li>
Both of the following commands print all lines containing strings
<b>abc</b>
or
<b>def</b>
or both:
<pre>
<code>
grep -E 'abc
def'

grep -F 'abc
def'
</code>
</pre>
<p>
<li>
Both of the following commands print all lines matching exactly
<b>abc</b>
or
<b>def</b>:
<pre>
<code>
grep -E '^abc$
^def$'

grep -F -x 'abc
def'
</code>
</pre>
<p>
</ol>
</blockquote><h4><a name = "tag_001_014_907">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_001_014_908">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="sed.html">sed</a></i>.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>
